//! # 可观测性模块集成测试

use rustcloud_observability::*;
use std::time::Duration;

#[tokio::test]
async fn test_observability_integration() {
    // 创建监控配置
    let config = monitoring::MonitoringConfig::default();
    let monitor = monitoring::InMemoryMonitor::new(config);
    
    // 启动监控
    monitor.start().await.unwrap();
    
    // 记录一些指标
    monitor.record_message_published("test.topic", Duration::from_millis(100)).await;
    monitor.record_message_processed("test.topic", true, Duration::from_millis(50)).await;
    
    // 获取指标
    let metrics = monitor.get_metrics().await.unwrap();
    assert_eq!(metrics.throughput.total_messages, 1);
    assert_eq!(metrics.throughput.successful_messages, 1);
    
    // 测试健康检查
    let health_result = monitor.health_check().await.unwrap();
    assert_eq!(health_result.status, health::HealthStatus::Healthy);
}

#[tokio::test]
async fn test_health_check_system() {
    let config = health::HealthCheckConfig::default();
    let mut manager = health::HealthCheckManager::new(config);
    
    // 注册健康检查器
    let checker = Box::new(health::DatabaseHealthChecker::new(
        "test_db".to_string(),
        "test://localhost".to_string()
    ));
    manager.register_checker("database".to_string(), checker);
    
    // 执行健康检查
    let result = manager.check_all().await.unwrap();
    assert!(result.components.contains_key("database"));
}

#[tokio::test]
async fn test_metrics_system() {
    let registry = metrics::MetricRegistry::new();
    
    // 注册计数器指标
    let counter = std::sync::Arc::new(metrics::Counter::new(
        "测试计数器".to_string(),
        std::collections::HashMap::new()
    ));
    registry.register_metric("test_counter".to_string(), counter.clone()).await.unwrap();
    
    // 增加计数
    counter.inc();
    counter.add(5);
    
    // 收集指标
    let samples = registry.collect_all().await.unwrap();
    assert!(!samples.is_empty());
}

#[tokio::test]
async fn test_alerting_system() {
    let mut manager = alerting::AlertManager::new();
    manager.add_handler(Box::new(alerting::LogAlertHandler));
    
    let alert = alerting::Alert {
        id: "test-001".to_string(),
        level: alerting::AlertLevel::Warning,
        title: "测试告警".to_string(),
        message: "这是一个测试告警".to_string(),
        timestamp: chrono::Utc::now(),
        data: std::collections::HashMap::new(),
    };
    
    // 处理告警
    assert!(manager.process_alert(alert).await.is_ok());
}